package org.tnc.budget.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.tnc.budget.dao.BudgetCenterDaoHibernate;
import org.tnc.budget.model.BudgetCenter;
import org.tnc.budget.service.BudgetManager;

/**
 * 预算中心管理器
 * 
 * @author houjj | 2011-4-4 下午04:06:48
 */
public class BudgetManagerImpl implements BudgetManager {
	private Logger log = Logger.getLogger(BudgetManagerImpl.class.getName());
	private BudgetCenterDaoHibernate budgetDao;

	public BudgetCenterDaoHibernate getBudgetDao() {
		return budgetDao;
	}

	public void setBudgetDao(BudgetCenterDaoHibernate budgetDao) {
		this.budgetDao = budgetDao;
	}

	public List<BudgetCenter> getAll() {
		return budgetDao.getValidBudgetCenterList(true, null);
	}
	
	@Override
	public List<BudgetCenter> getAll(boolean bcValid, Boolean vacation) {
		return budgetDao.getValidBudgetCenterList(bcValid, vacation);
	}

	@Override
	public void save(BudgetCenter budgetCenter) {
		budgetDao.save(budgetCenter);
	}

	@Override
	public void update(BudgetCenter budgetCenter) {
		budgetDao.saveOrUpdate(budgetCenter);
	}

	public boolean delete(int budgetId) {
		boolean result = false;

		BudgetCenter bc = this.budgetDao.get(budgetId);
		if (bc != null) {
			bc.setBcValid(false);
			budgetDao.saveOrUpdate(bc);

			result = true;
		} else {
			log.warn("Budget info not exist, id:"+budgetId);
		}

		return result;
	}

	@Override
	public BudgetCenter getBudgetById(int budgetId) {
		return this.budgetDao.get(budgetId);
	}

	@Override
	public BudgetCenter getBudgetByName(String budgetName) {
		return this.budgetDao.getBudgetByName(budgetName);
	}
	
	public BudgetCenter getBudgetByBcId(String bcId){
		return this.budgetDao.getBudgetByBcId(bcId);
	}

	@Override
	public List<BudgetCenter> findByDuty(Long userId) {
		return this.budgetDao.find("from BudgetCenter as bc where bc.user.id=?", new Object[]{userId});
	}

}
